﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_base';
</cfscript>

<!---注册--->
<cffunction name="reg" access="remote" returnFormat="json">
  <cfscript>
	success = 0;
	j = {"success":"0"};
	m_auth = 0;
	</cfscript>
  <cfif isdefined('mobile') and isdefined('userpwd')>
		<!---判断手机号是否注册--->
    <cfquery datasource="#DB#" name="checkmobile">
    select uid,uuid
    from #dbt#
    where mobile = '#mobile#'
    </cfquery>
		<cfif checkmobile.recordcount eq 0>
  
			<!---要求输入验证码--->
      <cfif isdefined('vcode')>
      
        <!---验证码输入--->
        <cfinvoke component="app.cfc.user_regvcode" method="checkvcodecuts" mobile="#mobile#"  vcode="#vcode#" returnvariable="checkvcodecuts" />
				<cfif checkvcodecuts.recordcount eq 0>
          <!---验证码不存在或已经过期--->
          <cfscript>
          success = 2;
          //j = {"success": success, "msg":"验证码不存在或已经过期"};
					j = {"success":"0","errCode":"2"};;
          </cfscript>
        <cfelseif vcode neq checkvcodecuts.vcode>
          <cfif checkvcodecuts.counts gte 3>
            <cfscript>
            success = 3;
            //j = {"success": success, "msg":"验证码输入错误达到3次"};
						j = {"success":"0","errCode":"2"};
            </cfscript>
            <cfquery datasource="#DB#">
            delete from user_regvcode
            where mobile = '#mobile#'
            </cfquery>
          <cfelse>
            <cfquery datasource="#DB#">
            update user_regvcode set
            counts = counts + 1
            where mobile = '#mobile#'
            </cfquery>
            <cfscript>
            success = 4;
            //j = {"success": success, "msg":"验证码输入错误"};
						j = {"success":"0","errCode":"2"};
            </cfscript>
          </cfif>
          
        <cfelse>
        	<cfset success = 1>
          
        </cfif>
      </cfif>
      
      <cfif success eq 1>
      	<cfscript>
				if (isdefined('vcode')){
					m_auth = 1;
				}
				UUID = CREATEUUID();
				MUUID = CREATEUUID();
				</cfscript>
				<!---正式注册--->
		<cfquery datasource="#DB#" result="addrs">
        insert into #dbt#(uuid,userpwd,reg_dt,mobile,m_auth,muuid,mobile_checked)
        values('#UUID#','#userpwd#',#now()#,'#mobile#',#m_auth#,'#MUUID#','1')
        </cfquery>
		<!---手机认证通过，送额度--->
        <cfif m_auth eq 1>
          <cfquery datasource="#DB#">
          insert into user_glftcredit(UID,glftlevel,credit,senddt,isEnabled,rundt)
          value(#addrs.GENERATEDKEY#,1,100,#now()#,1,#now()#)
          </cfquery>
        </cfif>
        
        
        
				<cfscript>
        success = 1;
        j = {"success": success, "uuid": MUUID};
        </cfscript>
      </cfif>
		
    <cfelse>  
			<cfscript>
      //j = {"success":"0","msg":"手机号码已注册"};
			j = {"success":"0","errCode":"1"};
      </cfscript>
      
    </cfif>
  
  
  <cfelse>
		<cfscript>
    //j = {"success":"0","msg":"缺少参数"};
		j = {"success":"0","errCode":"-1"};
    </cfscript>
  
  </cfif>
  
  
  
  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>

</cffunction>
  
</cfcomponent>